package com.android.lovingpet.adopt.host.hostinfo;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;


import com.android.lovingpet.adopt.host.HostModel;
import com.android.lovingpet.base.BasePresenter;

import com.android.lovingpet.bean.HostFoster;


import com.android.lovingpet.common.Back;
import com.android.lovingpet.common.PostModel;
import com.android.lovingpet.mine.pet.PetModel;


import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by ikata on 2018/5/21.
 */

public class HostInfoPresenter extends BasePresenter<IHostInfoContract.View>implements IHostInfoContract.Presenter{

    private PetModel petModel;
    private HostInfoModel hostInfoModel;
    private HostModel hostModel;

    public static final int FLAG_Undelivered_PET_ID =0;
    public static final int FLAG_Unback_PET_ID=2;
    public static final int FLAG_Unused_PET_ID=5;
    public static final int FLAG_HOST_FOSTER =1;
    public static final int FLAG_PET_INFO=3;
    public static final int FLAG_HOST_INFO=4;

    public HostInfoPresenter()
    {
        petModel=new PetModel();
        hostInfoModel=new HostInfoModel();
        hostModel=new HostModel();
    }

    @Override
    public void getUndeliveredPetId(int masterId, int offset, int num) {
        HashMap<String,String> params=new HashMap<>();
        params.put("petType", String.valueOf(0));
        params.put("chooseType", String.valueOf(1));
        params.put("masterId", String.valueOf(masterId));
        params.put("offset", String.valueOf(offset));
        params.put("num", String.valueOf(num));
        getPetId(params,FLAG_Undelivered_PET_ID);
    }

    @Override
    public void getUnusedPetId(int masterId, int offset, int num) {
        HashMap<String,String> params=new HashMap<>();
        params.put("petType", String.valueOf(0));
        params.put("chooseType", String.valueOf(3));
        params.put("masterId", String.valueOf(masterId));
        params.put("offset", String.valueOf(offset));
        params.put("num", String.valueOf(num));
        getPetId(params,FLAG_Unused_PET_ID);
    }

    @Override
    public void getUnbackedPetId(int masterId, int offset, int num) {
        HashMap<String,String> params=new HashMap<>();
        params.put("petType", String.valueOf(0));
        params.put("chooseType", String.valueOf(2));
        params.put("masterId", String.valueOf(masterId));
        params.put("offset", String.valueOf(offset));
        params.put("num", String.valueOf(num));
        getPetId(params,FLAG_Unback_PET_ID);
    }


    public void getPetId(HashMap<String,String> params,int flag)
    {
        petModel.getPetId(params, new Back() {
            @Override
            public void onFailure(String msg) {
                if (getView()!=null)
                    getView().showToast(msg);
            }
            @Override
            public void onSuccess(@Nullable Bundle bundle) {
                if (bundle==null)
                    return;
                ArrayList<Integer> ids=bundle.getIntegerArrayList("ids");
                Bundle bundle1=new Bundle();
                bundle1.putInt("flag", flag);
                bundle1.putIntegerArrayList("ids",ids);
                Log.d(flag+"",ids.toString());
                getView().next(bundle1);
            }
        });
    }




    @Override
    public void getPetInfo(int id, int petType) {
        HashMap<String,String> params=new HashMap<>();
        params.put("id", String.valueOf(id));
        params.put("petType", String.valueOf(petType));
        petModel.getPetInfo(params, new Back() {
            @Override
            public void onFailure(String msg) {
                if (getView()!=null)
                getView().showToast(msg);
            }
            @Override
            public void onSuccess(@Nullable Bundle bundle) {
                bundle.putInt("flag",FLAG_PET_INFO);
                if (getView()!=null)
                    getView().next(bundle);
            }
        });
    }

    @Override
    public void getHostFosterById(int id) {
        hostInfoModel.getHostFosterById(id, new Back() {
            @Override
            public void onFailure(String msg) {
                getView().showToast(msg);
            }
            @Override
            public void onSuccess(@Nullable Bundle bundle) {
                HostFoster hostFoster=bundle.getParcelable("result");
                Bundle bundle1=new Bundle();
                bundle1.putInt("flag", FLAG_HOST_FOSTER);
                bundle1.putParcelable("hostFoster",hostFoster);
                getView().next(bundle1);
            }
        });
    }
    public void fostersPet(int hostId,int fosterId,String beginTime,String endTime)
    {
        HashMap<String,String> params=new HashMap<>();
        params.put("hostId",String.valueOf(hostId));
        params.put("fosterId",String.valueOf(fosterId));
        params.put("beginTime",beginTime);
        params.put("endTime",endTime);
        hostInfoModel.fostersPet(params, new Back() {
            @Override
            public void onFailure(String msg) {
                getView().showToast(msg);
            }
            @Override
            public void onSuccess(@Nullable Bundle bundle) {
                if (bundle==null)
                {
                    getView().next(null);
                }
            }
        });
    }
    public void getHostInfo(int id)
    {
        HashMap<String,String> params=new HashMap<>();
        params.put("id", String.valueOf(id));
        hostModel.getHostInfo(params, new Back() {
            @Override
            public void onFailure(String msg) {

            }
            @Override
            public void onSuccess(@Nullable Bundle bundle) {
                if (getView()==null) {
                    return;
                }
                bundle.putInt("flag",FLAG_HOST_INFO);
                getView().next(bundle);
            }
        });

    }

    public void updateInfo(String beginTime,String endTime,int fosterId,int newFosterId)
    {
        HashMap<String,String> params=new HashMap<>();
        params.put("beginTime",beginTime);
        params.put("endTime",endTime);
        params.put("fosterId", String.valueOf(fosterId));
        params.put("newFosterId", String.valueOf(newFosterId));
        hostInfoModel.updateInfo(params, new Back() {
            @Override
            public void onFailure(String msg) {
                getView().showToast(msg);
            }

            @Override
            public void onSuccess(@Nullable Bundle bundle) {
                getView().next(null);
            }
        });

    }

    public void updateState(int fosterId,int state)
    {
        HashMap<String,String> params=new HashMap<>();
        params.put("fosterId", String.valueOf(fosterId));
        params.put("state", String.valueOf(state));
        hostInfoModel.updateState(params, new Back() {
            @Override
            public void onFailure(String msg) {
                getView().showToast(msg);
            }
            @Override
            //直接传递 bundle为null
            public void onSuccess(@Nullable Bundle bundle) {
             getView().next(bundle);
            }
        });

    }

    @Override
    public void evaluate(int hostId,String content, int evaluationType, int anonymous) {
        PostModel.getInstance().post(content, 0, 0, 2, evaluationType, anonymous, hostId, new Back() {
            @Override
            public void onFailure(String msg) {
                if (getView() == null) {
                    return;
                }
                getView().showToast(msg);
            }
            @Override
            public void onSuccess(@Nullable Bundle bundle) {
                getView().next(null);
            }
        });
    }

}
